package com.hulk.controller;

import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hulk.model.UserInfo;
import com.hulk.service.EhCacheService;
import com.hulk.service.UserService;

/**  
 * 创建时间：2015-1-28 下午1:17:27  
 * @author andy  
 * @version 2.2  
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private EhCacheService ehCacheService;
	
	@RequestMapping("/showInfo/{userId}")
	public String showUserInfo(ModelMap modelMap, @PathVariable int userId){
		UserInfo userInfo = userService.getUserById(userId);
		modelMap.addAttribute("userInfo", userInfo);
		return "/user/showInfo";
	}
	
	@RequestMapping("/getUserById/{userId}")
	public @ResponseBody Object getUserById(@PathVariable int userId){
		long date1 = System.currentTimeMillis();
		UserInfo userInfo = userService.getUserById(userId);
		long haoshi = System.currentTimeMillis() - date1;
		System.out.println(haoshi);
		return "time="+haoshi;
	}
	@RequestMapping("/rmUserById/{userId}")
	public @ResponseBody Object rmUserById(@PathVariable int userId){
		long date1 = System.currentTimeMillis();
		userService.rmUserById(userId);
		System.out.println(System.currentTimeMillis() - date1);
		return "rmUserById";
	}
	
	@RequestMapping("/showInfos")
	public @ResponseBody Object showUserInfos(){
		long date1 = System.currentTimeMillis();
		List<Map<String,Object>> userInfos = userService.getUsers();
		long haoshi = System.currentTimeMillis() - date1;
		System.out.println(haoshi);
		Cache cache = ehCacheService.getCache("myCache");
        System.out.println("cache.getKeys()="+cache.getKeys());
        for(Object obj : cache.getKeys()){
        	System.out.println(cache.get(obj).toString().length());
        }
		return "userInfos.size()="+userInfos.size()+",time="+haoshi;
	}
	
	@RequestMapping("/rmUsers")
	public @ResponseBody Object rmUsers(){
		long date1 = System.currentTimeMillis();
		userService.removeAll();
		System.out.println(System.currentTimeMillis() - date1);
		return "rmAll";
	}

	/** 删除全部缓存 */
	@RequestMapping("/removeAllEhCache")
//	@SystemControllerLog(description = "删除全部缓存")
	public @ResponseBody Object  removeAllEhCache(String cacheNames) {
		JSONObject jsonObject =	null;
		try{
			jsonObject = this.userService.removeAllEhCache(cacheNames);
		}catch(Exception e){
		}
		return jsonObject;
	}
	
	/** 查询全部缓存 */
	@RequestMapping("/getAllEhCache")
//	@SystemControllerLog(description = "查询全部缓存")
	public @ResponseBody Object  getAllEhCache(String cacheNames) {
		JSONObject jsonObject =	null;
		try{
			jsonObject = this.userService.getAllEhCache(cacheNames);
		}catch(Exception e){
		}
		return jsonObject;
	}
	
	
	@RequestMapping("/getCache")
	public @ResponseBody Object getCache(String cacheKey) throws InterruptedException{
//		Map re = userService.getCache();
//		ehCacheService.setObject("1=1","myCache", "getCache", null);
//		String re = ehCacheService.getObject("myCache", "getCache", null).toString();
//		System.out.println(ehCacheService.getObject("myCache", "getCache", null));
//		re = re+"=2";
//		System.out.println(ehCacheService.getObject("myCache", "getCache", null));
//		int a = 0;
//		ehCacheService.setObject("我的20秒", "myCache", "mykey", null);
//		while(a < 5){
////			System.out.println(ehCacheService.getCache("myCache"));
//			System.out.println(ehCacheService.getObject("myCache", "mykey", null));
//			a++;
//			Thread.sleep(10000);
////			ehCacheService.setObject("我的20秒"+a, "myCache", "mykey", null);
//		}
		Object result = null;
		while(result == null){
			Thread.sleep(500);
			result = ehCacheService.getObject("myCache", cacheKey, null);
			System.out.println("result="+result);
		}
		System.out.println("已获取集群数据="+result);
		return result;
	}

	@RequestMapping("/putCache")
	public @ResponseBody Object putCache(String cacheKey,String cacheValue) throws InterruptedException{
		ehCacheService.setObject(cacheValue,"myCache", cacheKey, null);
		Object result = ehCacheService.getObject("myCache", cacheKey, null);
		return result;
	}
}
